from report import report_sxw


class Parser(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context):
        super(Parser, self).__init__(cr, uid, name, context)
        self.localcontext.update({

            'lines_f': self.get_lines_form,
            'total_f': self.get_total_form,
        })
        self.context = context

#### Report in Form


    def get_lines_form(self, sale_id):
        cr = self.cr
        uid = self.uid
        result = []
        sale_obj = self.pool.get('sale.order')
        product_obj = self.pool.get('product.product')
        if sale_id:
            lines = sale_obj.browse(cr, uid, sale_id).order_line
            lst_product_id = [line and line.product_id and line.product_id.id for line in lines]
            lst_product_id = list(set(lst_product_id))
            lst_categ = [pro.categ_id for pro in product_obj.browse(cr, uid, lst_product_id)]
            lst_categ = list(set(lst_categ))
            for categ in lst_categ:
                name = categ.name
                qty_product = 0
                qty_pack = 0
                weight_product = 0
                weight_pack = 0
                weight = 0
                for line in lines:
                    if line and line.product_id and line.product_id.categ_id.id == categ.id:
                        qty_product += line.product_uom_qty
                        weight_product += line.product_id.weight * line.product_uom_qty
                        if line.product_packaging and line.product_packaging > 0:
                            pack = line.product_packaging
                            qty_pack += pack.qty != 0 and line.product_uom_qty / pack.qty
                            weight_pack += pack.qty != 0 and pack.weight_ul * line.product_uom_qty / pack.qty

                        weight = weight_product + weight_pack
                res = {
                    'name': name,
                    'qty_product': qty_product,
                    'qty_pack': qty_pack,
                    'weight': weight,
                }
                result.append(res)
        return result

    def get_total_form(self, sale_id):
        cr = self.cr
        uid = self.uid
        result = []
        sale_obj = self.pool.get('sale.order')
        if sale_id:
            lines = sale_obj.browse(cr, uid, sale_id).order_line
            qty_product = 0
            qty_pack = 0
            weight_product = 0
            weight_pack = 0
            weight = 0
            for line in lines:
                qty_product += line.product_uom_qty
                weight_product += line.product_id.weight * line.product_uom_qty
                if line.product_packaging and line.product_packaging > 0:
                    pack = line.product_packaging
                    qty_pack += pack.qty != 0 and line.product_uom_qty / pack.qty
                    weight_pack += pack.qty != 0 and pack.weight_ul * line.product_uom_qty / pack.qty

                weight = weight_product + weight_pack
            res = {
                'qty_product': qty_product,
                'qty_pack': qty_pack,
                'weight': weight,
            }
            result.append(res)
        return result
